package org.nanquan.dotjava.view.filter;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

public class HtmlFilter implements Filter {

	private PageManager pageManager = PageManager.getManager();
	
	private Boolean isCreate=true;
	
	private Boolean defaultCreate =true;

	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		
		if(!isCreate)
		{
			arg2.doFilter(request, response);
			return ;
		}

		HttpServletRequest httpRequest = (HttpServletRequest) request;
		HttpServletResponse httpResponset = (HttpServletResponse) response;
		
//		String requestPath = httpRequest.getRequestURI(); //    /webSite/admin/index.jsp首页时没有资源名
		String webSite=httpRequest.getContextPath(); //webUrl(默认工程�?) /webSite
		String servletPath=httpRequest.getServletPath();//资源�? �? /index.jsp
		String requestPath=webSite+servletPath;         //网站�?+资源�?  �?  /webSite/admin/index.jsp
		String queryString=null;							//url参数  如id=1&name=aa
		String allPath="";				//全路�? (网站�?+资源�?+url参数) �?  /webSite/admin/index.jsp?id=1&name=aa
		String htmlPath=null;  			//将要生成的html路径
		
		Boolean create=true;

		//获取url参数
		if (httpRequest.getQueryString() != null) {
			queryString = httpRequest.getQueryString();
		}
		allPath=queryString==null?requestPath:requestPath+"?"+queryString;
		//判断是否已生成静态页
		if (pageManager.containsPage(allPath)) {
			htmlPath=pageManager.getHtmlPage(allPath);
		}else if(pageManager.isBlackList(servletPath)){
			create=false;
		}else if(pageManager.isWhiteList(servletPath)){
			CreateHtml createHtml=new CreateHtml();
			htmlPath=createHtml.creat(allPath,httpRequest, httpResponset);
			pageManager.addHtmlPage(allPath, htmlPath);
		}else if(pageManager.isBlackFolder(servletPath)){
			create=false;
		}
		else if(pageManager.isWhiteFolder(servletPath)){
			if(pageManager.isWhiteExe(servletPath))
			{
				CreateHtml createHtml=new CreateHtml();
				htmlPath=createHtml.creat(allPath,httpRequest, httpResponset);
				pageManager.addHtmlPage(allPath, htmlPath);
			}else if(pageManager.isBlackExe(servletPath))
			{
				create=false;
			}else
			{
				CreateHtml createHtml=new CreateHtml();
				htmlPath=createHtml.creat(allPath,httpRequest, httpResponset);
				pageManager.addHtmlPage(allPath, htmlPath);
			}
		}
		
		if(htmlPath!=null)
		{
			httpResponset.sendRedirect(htmlPath);
		}
		else if(create&&defaultCreate&&!pageManager.isBlackExe(servletPath))
		{
			CreateHtml createHtml=new CreateHtml();
			htmlPath=createHtml.creat(allPath,httpRequest, httpResponset);
			pageManager.addHtmlPage(allPath, htmlPath);
			httpResponset.sendRedirect(htmlPath);
		}else
		{
			arg2.doFilter(request, response);
		}
	}

	public void init(FilterConfig config) throws ServletException {
		
		if(config.getInitParameter("black_exe")!=null)
		{
			pageManager.addBlackExeBySpliString(config.getInitParameter("black_exe"));
		}
		
		if(config.getInitParameter("white_exe")!=null)
		{
			pageManager.addWhiteExeBySpliString(config.getInitParameter("white_exe"));
		}
		
		if(config.getInitParameter("white_list")!=null)
		{
			pageManager.addWhiteListBySpliString(config.getInitParameter("white_list"));
		}
		
		if(config.getInitParameter("black_list")!=null)
		{
			pageManager.addBlackListBySpliString(config.getInitParameter("black_list"));
		}
		
		if(config.getInitParameter("black_folder")!=null)
		{
			pageManager.addBlackFolderBySpliString(config.getInitParameter("black_folder"));
		}
		
		if(config.getInitParameter("white_folder")!=null)
		{
			pageManager.addWhiteFolderBySpliString(config.getInitParameter("white_folder"));
		}
		
		if(config.getInitParameter("isrun")!=null)
		{
			String parameter = config.getInitParameter("isrun");
			isCreate=parameter.equals("yes")||parameter.equals("true")||parameter.equals("1")?true:false;
		}
		if(config.getInitParameter("default_create")!=null)
		{
			String parameter = config.getInitParameter("default_create");
			defaultCreate=parameter.equals("yes")||parameter.equals("true")||parameter.equals("1")?true:false;
		}
		
	}

}
